var ZYRequestClient = require("../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data: {
    adminStampNotice:'',
    phoneNumber:null,
    showLogin:true,
    showPhoneNumber:false,
    dialogShow: false,
    checked:false,
    userInfo: {},
    code:null,
    openId:null,
    sessionKey:null,
    year:'',
    navbarData:{
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '登录', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
    var now = new Date();
    var year = now.getFullYear();
    this.setData({
      year:year
    });
  },
  bindAgreeChange(e){
    var checked = e.detail.value[0] == "yes" ? true:false;
    this.setData({
      checked:checked
    });
  },
  // 微信授权登录
  getUserProfile(e) {
    var that = this;
    // 判断是否同意相关条款
    if(!that.data.checked){
      wx.showToast({
        title: '请阅读并同意相关条款！',
        icon:"none"
      })
      return;
    }
    wx.getUserProfile({
      lang:'zh_CN',
      desc: '用于完善您的个人资料',
      success: (user) => {
        // 微信登录，获取code
        wx.login({
          timeout: 3000,
          success:function(res){
              // 获取openid和session_key
              requestClient.getOpenIdAndSessionKey(res.code,function(response){
                that.setData({
                  openId:response.data.openid,
                  sessionKey:response.data.session_key
                });
                // 获取用户信息
                requestClient.getUserInfo(that.data.openId,function(userData){
                    if(userData.data.data){
                        if(userData.data.data.phoneNumber){
                          // 更新头像，性别信息
                          // userData.data.data.nickName = user.userInfo.nickName;
                          userData.data.data.avatarUrl = user.userInfo.avatarUrl;
                          userData.data.data.gender = user.userInfo.gender;
                          // 同步用户信息
                          requestClient.syncUserInfo(userData.data.data,function(res){
                              // 存储用户信息
                              wx.setStorageSync('userInfo', userData.data.data);
                              // 跳转页面
                              wx.redirectTo({
                                url: '../buildorg/index',
                              });
                          },function(){
                            wx.showModal({
                              title:"授权登录失败！",
                              content:'昵称包含特殊字符，您可以新建用户信息重新授权！',
                              showCancel:false,
                              success(r){
                  
                              }
                            })
                          })
                          return;
                        }
                    }
                    that.setData({
                      userInfo: user.userInfo,
                      showLogin:false,
                      showPhoneNumber: true,
                      code:res.code
                    });
                });
              });
          }
        })
      }
    })
  },
  // 绑定用户手机号码
  getPhoneNumber(e){
    var that = this;
    if(e.detail.errMsg == "getPhoneNumber:ok"){
      if(!!e.detail.code){
        // 解码用户手机号码
        requestClient.getUserPhoneNumber(e.detail.code,function(response){
          if(response.data.data == null){
            wx.showModal({
              title: "绑定失败",
              content: '获取手机号码异常，请退出小程序重试！',
              showCancel: false,
              success(r) {
                
              }
            })
            return;
          }
          that.setData({
            phoneNumber: response.data.data,
            dialogShow: true
          })
        });
      } else {
        wx.showModal({
          title: "绑定失败",
          content: '当前微信版本不支持，请检查更新微信版本',
          showCancel: false,
          success(r) {
            
          }
        })
      }
    }
  },
  formStampNoticeChange(e) {
    var _this = this;
    _this.setData({
      adminStampNotice: e.detail.value,
    });
  },
  tapDialogButton(e) {
    var that = this;
    if (that.data.adminStampNotice == '') {
      wx.showToast({
        title: '请填写真实姓名',
        icon: 'none'
      })     
    } else {
      if (e.detail.index == 1) {
        this.setData({
          dialogShow: false,
        })
        // 封装用户对象
        var userData = {
          openId: that.data.openId,
          nickName: that.data.adminStampNotice,
          avatarUrl: that.data.userInfo.avatarUrl,
          gender: that.data.userInfo.gender,
          phoneNumber: that.data.phoneNumber
        }
        // 注册用户信息
        requestClient.register(userData, function (userData) {
          // 存储用户信息
          wx.setStorageSync('userInfo', userData.data.data);
          // 跳转页面
          wx.redirectTo({
            url: '../buildorg/index',
          })
        }, function () {
          wx.showModal({
            title: "授权登录失败！",
            content: '昵称包含特殊字符，您可以新建用户信息重新授权！',
            showCancel: false,
            success(r) {
              if (r.confirm) {
                that.setData({
                  showLogin: true,
                  showPhoneNumber: false,
                });
              }
            }
          })
        });
      }      
    }    
  },
  preventClick:function(){
    
  },
  // 跳过
  bindSkip(e){
    var operate = parseInt(e.currentTarget.dataset.operate);
    if(operate == 1){
      // 跳转首页
      wx.switchTab({
        url: '../tabbar/home/index',
      })
    } else {
      // 完善组织架构
      wx.redirectTo({
        url: '../buildorg/index',
      });
    }
  }
})